package com.quasar.hpatient.dialog;

import android.app.Activity;
import android.text.TextUtils;
import android.widget.TextView;

import com.quasar.hpatient.R;

import lib.quasar.base.dialog.BaseDialog;
import lib.quasar.util.InputUtil;
import lib.quasar.util.ToastUtil;
import lib.quasar.widget.edit.AlterEditText;
import lib.quasar.widget.menu.NavigationView;

/**
 * description: 单个输入框
 * created by kalu on 2018/4/9 11:10
 */
public class InputDialog extends BaseDialog {

    private String mText = "";
    /******************************************************/

    private OnDialogChangeListener listener;

    public InputDialog(Activity activity) {
        super(activity);
    }

    @Override
    protected int initView() {
        return R.layout.layout_dialog_input;
    }

    @Override
    protected void initData() {

        final NavigationView menu = findViewById(R.id.dialog_input_single_menu);
        menu.setOnNavigationChangeListener((isBack, isMenu) -> {
            if (isBack) cancel();
            else if (isMenu) {

                if (null != listener) {

                    final AlterEditText edit = findViewById(R.id.dialog_input_single_input);
                    final String trim = edit.getText().toString();

                    if (!TextUtils.isEmpty(trim) ) {

                        final float temp = Float.parseFloat(trim);
                        if (temp < edit.getMinInput() || temp > edit.getMaxInput()) {
                            ToastUtil.showToast("输入数值不正确");
                            return;
                        }
                        listener.onDialogChange(trim);
                    } else {
                        ToastUtil.showToast("输入数值不正确");
                    }
                }

                cancel();
            }
        });
    }

    @Override
    public void show() {
        super.show();

        final AlterEditText text = findViewById(R.id.dialog_input_single_input);
        if (null == text) return;
        text.requestFocus();
        InputUtil.openKeybord(text);
    }

    public void setInputFilter(int dot,int num){
        final AlterEditText text = findViewById(R.id.dialog_input_single_input);
        text.setNumberFilter(dot,num);

    }

    @Override
    public void dismiss() {

        final AlterEditText text = findViewById(R.id.dialog_input_single_input);
        if (null != text) {
            InputUtil.closeKeybord(text);
        }

        super.dismiss();
    }

    public final void setTities(String str) {
        final NavigationView text = findViewById(R.id.dialog_input_single_menu);
        if (null == text) return;
        text.setTitle(str);
    }

    public final void setMenu(String str) {
        final TextView text = findViewById(R.id.dialog_input_single_text);
        if (null == text) return;
        text.setText(str);
    }

    public final void setHint(String str) {
        final AlterEditText text = findViewById(R.id.dialog_input_single_input);
        if (null == text) return;
        text.setHint(str);
    }

    public final void setInput(String str) {
        final AlterEditText text = findViewById(R.id.dialog_input_single_input);
        if (null == text) return;
        mText = str;
        text.setText(str);
        text.setSelection(str.length());

    }

    public final void setMax(float max) {
        final AlterEditText text = findViewById(R.id.dialog_input_single_input);
        if (null == text) return;
        text.setMaxInput(max);
    }

    public final void setMin(float min) {
        final AlterEditText text = findViewById(R.id.dialog_input_single_input);
        if (null == text) return;
        text.setMinWarning(min);
    }

    public final void setDotLength(int length) {
//        final AlterEditText text = findViewById(R.id.dialog_input_single_input);
//        if (null == text) return;
//        text.setDotLength(length);
    }

    public final void setMaxLength(int length) {
        final AlterEditText text = findViewById(R.id.dialog_input_single_input);
        if (null == text) return;
        text.setMaxLength(length);
    }

    public final void setInputType(int type) {
        final AlterEditText text = findViewById(R.id.dialog_input_single_input);
        if (null == text) return;
        text.setInputType(type);
    }

    public void setOnDialogChangeListener(OnDialogChangeListener mListener) {
        this.listener = mListener;
    }

    public interface OnDialogChangeListener {

        void onDialogChange(String input);
    }
}
